//
//  LCMineService.m
//  lechuang
//
//  Created by Mr_hi on 2023/3/25.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCMineService.h"
#import "XWXNetWorkQuery.h"
#import "LCUserModel.h"
#import "LCRankingModel.h"
#import "LCFirendModel.h"
#import "LCAmountListModel.h"

@implementation LCMineService

//获取用户信息
+ (void)getUserInfoWithShowError:(BOOL)showIt Block:(void(^)(BOOL isSuccess, LCUserModel * userInfo))block {
    [[XWXNetWorkQuery sharedManage] AFRequestData:Url(URLUserData) HttpMethod:KGet HaveToken:YES params:@{}.mutableCopy completionHandle:^(id  _Nonnull result) {
        if ([[result objectForKey:@"success"] boolValue]) {
            LCUserModel * userInfo = [LCUserModel mj_objectWithKeyValues:result[@"field"]];
            LCUserDefSet(userInfo.mj_keyValues, UserInfo);
            LCUserDefSet(userInfo.userthumb, UserImg);
            LCUserDefSet(userInfo.nickname, UserName);
            block(YES, userInfo);
        } else {
            if (showIt) {
                [self showErrorText:[result objectForKey:@"tips"]];
            }
            block(NO, nil);
        }
    } errorHandle:^(NSError * _Nonnull error) {
        if (showIt) {
            XWXNetError
        }
        block(NO, nil);
    }];
}

//修改登陆密码
+ (void)changePswdWithOldPswd:(NSString *)oldPswd Pswd:(NSString *)pswd SurePswd:(NSString *)surePswd Block:(void(^)(BOOL isSuccess))block {
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    param[@"Password"] = pswd;
    param[@"SurePassword"] = surePswd;
    param[@"isOldPassword"] = @"1";
    param[@"OldPassword"] = oldPswd;
    
    [[XWXNetWorkQuery sharedManage] AFRequestData:Url(URLChangePswd) HttpMethod:KPost HaveToken:YES params:param completionHandle:^(id  _Nonnull result) {
        if ([[result objectForKey:@"success"] boolValue]) {
            LCSendNoti(@"LoadUserInfoNotification");
            [self showSuccessText:@"修改登陆密码成功!"];
            block(YES);
        } else {
            [self showErrorText:[result objectForKey:@"tips"]];
            block(NO);
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError
        block(NO);
    }];
}

//修改用户头像
+ (void)changeThumbWithImg:(UIImage *)image Block:(void(^)(BOOL isSuccess))block {
    [[XWXNetWorkQuery sharedManage] AfPostUrl:Url(URLChangeImg) params:@{}.mutableCopy HaveToken:YES imgsData:@{@"Thumb":image}.mutableCopy completionHandle:^(id  _Nonnull result) {
        if ([[result objectForKey:@"success"] boolValue]) {
            LCSendNoti(@"LoadUserInfoNotification");
            [self showSuccessText:@"更换头像成功!"];
            block(YES);
        } else {
            [self showErrorText:[result objectForKey:@"tips"]];
            block(NO);
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError
        block(NO);
    }];
}

//修改用户昵称
+ (void)changeNickName:(NSString *)nickName Block:(void(^)(BOOL isSuccess))block {
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    param[@"Nickname"] = nickName;
    [[XWXNetWorkQuery sharedManage] AFRequestData:Url(URLChangeNick) HttpMethod:KPost HaveToken:YES params:param completionHandle:^(id  _Nonnull result) {
        if ([[result objectForKey:@"success"] boolValue]) {
            LCSendNoti(@"LoadUserInfoNotification");
            [self showSuccessText:@"修改昵称成功!"];
            block(YES);
        } else {
            [self showErrorText:[result objectForKey:@"tips"]];
            block(NO);
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError
        block(NO);
    }];
}

//提现
+ (void)withdrawalWithModel:(NSString *)model CashAmount:(NSString *)amount Block:(void(^)(BOOL isSuccess))block {
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    param[@"WithdrawalMode"] = model;
    param[@"AssetsKey"] = @"UserAmount";
    param[@"CashAmount"] = amount;
    
    [[XWXNetWorkQuery sharedManage] AFRequestData:Url(URLWithdrawal) HttpMethod:KPost HaveToken:YES params:param completionHandle:^(id  _Nonnull result) {
        if ([[result objectForKey:@"success"] boolValue]) {
            LCSendNoti(@"LoadUserInfoNotification");
            [self showSuccessText:@"申请提现成功"];
            block(YES);
        } else {
            [self showErrorText:[result objectForKey:@"tips"]];
            block(NO);
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError
        block(NO);
    }];
}

//账户明细
+ (void)amountAllListWithPage:(NSInteger)page Block:(void(^)(BOOL isSuccess, NSArray <LCAmountListModel *>* list, NSInteger total))block {
    
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    param[@"AssetsKey"] = @"UserAmount";
    param[@"page"] = @(page);
    
    [[XWXNetWorkQuery sharedManage] AFRequestData:Url(URLAmountAllList) HttpMethod:KGet HaveToken:YES params:param completionHandle:^(id  _Nonnull result) {
        if ([[result objectForKey:@"success"] boolValue]) {
            NSArray * list = [LCAmountListModel mj_objectArrayWithKeyValuesArray:result[@"result"]];
            NSInteger total = [result[@"record"] integerValue];
            block(YES, list, total);
        } else {
            [self showErrorText:[result objectForKey:@"tips"]];
            block(NO, nil, 0);
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError
        block(NO, nil, 0);
    }];
}

//账户任务明细
+ (void)amountTaskListWithPage:(NSInteger)page Block:(void(^)(BOOL isSuccess, NSArray <LCAmountListModel *>* list, NSInteger total))block {
    
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    param[@"page"] = @(page);
    
    [[XWXNetWorkQuery sharedManage] AFRequestData:Url(URLAmountTaskList) HttpMethod:KGet HaveToken:YES params:param completionHandle:^(id  _Nonnull result) {
        if ([[result objectForKey:@"success"] boolValue]) {
            NSArray * list = [LCAmountListModel mj_objectArrayWithKeyValuesArray:result[@"result"]];
            NSInteger total = [result[@"record"] integerValue];
            block(YES, list, total);
        } else {
            [self showErrorText:[result objectForKey:@"tips"]];
            block(NO, nil, 0);
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError
        block(NO, nil, 0);
    }];
    
}

//账户提现明细
+ (void)amountTixianListWithPage:(NSInteger)page Block:(void(^)(BOOL isSuccess, NSArray <LCAmountListModel *>* list, NSInteger total))block {
    
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    param[@"page"] = @(page);
    
    [[XWXNetWorkQuery sharedManage] AFRequestData:Url(URLAmountTixianList) HttpMethod:KGet HaveToken:YES params:param completionHandle:^(id  _Nonnull result) {
        if ([[result objectForKey:@"success"] boolValue]) {
            NSArray * list = [LCAmountListModel mj_objectArrayWithKeyValuesArray:result[@"result"]];
            NSInteger total = [result[@"record"] integerValue];
            block(YES, list, total);
        } else {
            [self showErrorText:[result objectForKey:@"tips"]];
            block(NO, nil, 0);
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError
        block(NO, nil, 0);
    }];
}

//绑定支付宝信息
+ (void)bingAlipayInfoWithChar:(NSString *)aliChar Name:(NSString *)name Block:(void(^)(BOOL isSuccess))block {
    NSMutableDictionary *param = [NSMutableDictionary new];
    param[@"AlipayChar"] = aliChar;
    param[@"Alipayname"] = name;
    [[XWXNetWorkQuery sharedManage] AFRequestData:Url(URLBDailpay) HttpMethod:KGet HaveToken:YES params:param completionHandle:^(id  _Nonnull result) {
        if ([[result objectForKey:@"success"] boolValue]) {
            LCSendNoti(@"LoadUserInfoNotification");
            [self showSuccessText:@"绑定支付宝成功!"];
            block(YES);
        } else {
            [self showErrorText:[result objectForKey:@"tips"]];
            block(NO);
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError
        block(NO);
    }];
}

// 获取排行榜信息
+ (void)rankingListWithPage:(NSInteger)page Block:(void(^)(BOOL isSuccess, NSArray <LCRankingModel *>* list, NSInteger total))block {
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    param[@"page"] = @(page);
    
    [[XWXNetWorkQuery sharedManage] AFRequestData:Url(URLRankingList) HttpMethod:KGet HaveToken:YES params:param completionHandle:^(id  _Nonnull result) {
        if ([[result objectForKey:@"success"] boolValue]) {
            NSArray * list = [LCRankingModel mj_objectArrayWithKeyValuesArray:result[@"result"]];
            NSInteger total = [result[@"record"] integerValue];
            block(YES, list, total);
        } else {
            [self showErrorText:[result objectForKey:@"tips"]];
            block(NO, nil, 0);
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError
        block(NO, nil, 0);
    }];
}

//提交用户反馈
+ (void)userGuestWithContext:(NSString *)context Block:(void(^)(BOOL isSuccess))block {
    LCUserModel * userData = [LCUserModel mj_objectWithKeyValues:LCUserDefGet(UserInfo)];
    NSMutableDictionary *param = [NSMutableDictionary new];
    param[@"strTitle"] = @"投诉反馈";
    param[@"strMobile"] = userData.usermobile;
    param[@"strContext"] = context;
    [[XWXNetWorkQuery sharedManage] AFRequestData:Url(URLGuest) HttpMethod:KPost HaveToken:YES params:param completionHandle:^(id  _Nonnull result) {
        if ([[result objectForKey:@"success"] boolValue]) {
            [self showSuccessText:@"反馈成功"];
            block(YES);
        } else {
            [self showErrorText:[result objectForKey:@"tips"]];
            block(NO);
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError
        block(NO);
    }];
}

//获取邀请统计
+ (void)friendNumberWithBlock:(void(^)(BOOL isSuccess, NSInteger todayNum, NSInteger totalNum, NSInteger todayAmount))block {
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    [[XWXNetWorkQuery sharedManage] AFRequestData:Url(URLFriendNum) HttpMethod:KGet HaveToken:YES params:param completionHandle:^(id  _Nonnull result) {
        if ([[result objectForKey:@"success"] boolValue]) {
            NSDictionary * field = result[@"field"];
            NSInteger todayNum = [field[@"todaynum"] integerValue];
            NSInteger totalNum = [field[@"ischild"] integerValue];
            NSInteger todayAmount = [field[@"todayamount"] integerValue];
            block(YES, todayNum, totalNum, todayAmount);
        } else {
            [self showErrorText:[result objectForKey:@"tips"]];
            block(NO, 0, 0, 0);
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError
        block(NO, 0, 0, 0);
    }];
}

//获取我的邀请记录
+ (void)friendListWithPage:(NSInteger)page Block:(void(^)(BOOL isSuccess, NSArray <LCFirendModel *>* list, NSInteger total))block {
    
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    param[@"page"] = @(page);
    
    [[XWXNetWorkQuery sharedManage] AFRequestData:Url(URLFriendList) HttpMethod:KGet HaveToken:YES params:param completionHandle:^(id  _Nonnull result) {
        if ([[result objectForKey:@"success"] boolValue]) {
            NSArray * list = [LCFirendModel mj_objectArrayWithKeyValuesArray:result[@"result"]];
            NSInteger total = [result[@"record"] integerValue];
            block(YES, list, total);
        } else {
            [self showErrorText:[result objectForKey:@"tips"]];
            block(NO, nil, 0);
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError
        block(NO, nil, 0);
    }];
}


//检测版本
+ (void)checkVersionWirhBlock:(void(^)(BOOL isSuccess, BOOL hasNewV))block {
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    param[@"DeviceType"] = @"iOS";
    [[XWXNetWorkQuery sharedManage] AFRequestData:Url(URLVersion) HttpMethod:KGet HaveToken:NO params:param completionHandle:^(id  _Nonnull result) {
        if ([[result objectForKey:@"success"] boolValue]) {
            NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
            if ([version isEqualToString:result[@"field"][@"edition"]]) {
                [self showSuccessText:@"已是最新版本"];
                block(YES, NO);
            } else {
                block(YES, YES);
            }
        } else {
            [self showErrorText:[result objectForKey:@"tips"]];
            block(NO, NO);
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError
        block(NO, NO);
    }];
}


+ (void)getContactInfoBlock:(void(^)(BOOL isSuccess, NSString * tel, NSString *email, NSString *wxchar, NSString *qqchar))block {
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    param[@"businessid"] = @"1000011";
    NSString *force = [LCFunction ns_md5:@"贴牌代理-|-|-1000011-|-|-贴牌代理"];
    param[@"businesskey"] = force;
    
    [[XWXNetWorkQuery sharedManage] AFRequestData:Url(URLContact) HttpMethod:KGet HaveToken:NO params:param completionHandle:^(id  _Nonnull result) {
        if ([[result objectForKey:@"success"] boolValue]) {
            NSDictionary * contact = result[@"field"];
            NSString * tel = contact[@"customertel"];
            NSString * email = contact[@"customeremail"];
            NSString * wxchar = contact[@"weixinchar"];
            NSString * qqchar = contact[@"qqchar"];
            block(YES, tel, email, wxchar, qqchar);
        } else {
            [self showErrorText:[result objectForKey:@"tips"]];
            block(NO, nil, nil, nil, nil);
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError
        block(NO, nil, nil, nil, nil);
    }];
    
    
}

@end
